package com.iweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iweb.mapper.PowerMapper;
import com.iweb.mapper.RoleMapper;
import com.iweb.mapper.UserMapper;
import com.iweb.model.Power;
import com.iweb.model.Role;
import com.iweb.model.UserDto;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作者: jack
 * 时间: 2020-09-10 0010 10:15
 * 描述: UserDetailsServiceImpl
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private PowerMapper powerMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 根据用户名查询用户
        QueryWrapper<UserDto> userDtoQueryWrapper = new QueryWrapper<>();
        userDtoQueryWrapper.eq("username", username);
        UserDto userDto = userMapper.selectOne(userDtoQueryWrapper);
        // 根据用户id查询权限列表
        List<Power> authorizes = powerMapper.findAuthorizes(userDto.getId());
        // 封装UserDetails
        userDto.setAuthorities(authorizes);
        return User.withUserDetails(userDto).build();
    }
}
